package com.coffee.middleware.dict;


import cn.hutool.core.collection.CollectionUtil;
import com.coffee.core.CoffeeResponse;
import com.coffee.core.CoffeeResponseEnum;
import com.coffee.core.ValidGroup;
import com.coffee.middleware.dict.entity.DictTypeEntity;
import com.coffee.middleware.dict.entity.DictTypeVO;
import com.coffee.middleware.dict.repository.DictTypeRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Songkui
 * @since 2024-04-29
 */
@Tag(name = "字典类型管理")
@Slf4j
@RestController
@RequestMapping("/coffee/dict/type")
@AllArgsConstructor
public class DictTypeEndpoint {

    private final DictTypeRepository dictTypeRepository;

    @Operation(summary = "新增")
    @PostMapping("/insert")
    public CoffeeResponse<Integer> insert(@Validated(ValidGroup.Create.class) @RequestBody DictTypeEntity entity){
        Integer id = dictTypeRepository.saveDict(entity);
        return CoffeeResponse.responseSuccess(id);
    }

    @PutMapping("/update")
    public CoffeeResponse<?> update(@Validated(ValidGroup.Update.class) @RequestBody DictTypeEntity entity){
        dictTypeRepository.updateDict(entity);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    @PostMapping("/list")
    public CoffeeResponse<List<DictTypeVO>> search(){
        List<DictTypeVO> list = dictTypeRepository.listDictTypeVO();
        if (CollectionUtil.isEmpty(list)){
            return CoffeeResponse.response(CoffeeResponseEnum.NotData);
        }
        return CoffeeResponse.responseSuccess(list);
    }

    @GetMapping("/get/{id}")
    public CoffeeResponse<DictTypeEntity> get(@PathVariable("id") Integer id){
        return CoffeeResponse.responseSuccess(dictTypeRepository.getById(id));
    }

    @DeleteMapping("/delete/{id}")
    public CoffeeResponse<?> delete(@PathVariable("id") Integer id){
        dictTypeRepository.removeById(id);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }



}
